package com.corpdata.app.project.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.corpdata.core.base.AbstractBaseService;
import com.corpdata.app.project.dao.ProjectDoorMapper;
import com.corpdata.app.project.entity.ProjectDoor;
import com.corpdata.app.project.entity.ProjectTeam;
import com.corpdata.app.project.service.ProjectDoorService;
import com.corpdata.common.utils.HttpUtils;

/**
 * 
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-08-06 10:13:22
 */
@Service
@Transactional
public class ProjectDoorServiceImpl extends AbstractBaseService<ProjectDoor> implements ProjectDoorService {
	
	private static String cid = "880001001007616";// 客户号
	private static String usr = "880001001007616";//账号
	private static String psw = "880001001007616";//密码
	private static String signKey = "2bb447142776d94fd7b9b51e5e9622d2";// 签名signKey
	private static String v = "2";//接口版本号
	private static String areaCode = "0000007525";
	
	@Autowired
	private ProjectDoorMapper projectDoorMapper;
	@Override
	public String getQrcode(String personCode,String sd,String ed,String st,String et,String visitorName,String visitorTel) {
		// TODO Auto-generated method stub
		String serviceId = "3c.visitor.invite";
		String token = getToken();
		HashMap<String, String> params = new HashMap<String, String>();
		String url = "http://www.jslife.com.cn/jsaims/as";
		String p = "{\"serviceId\":\"" + serviceId+ "\",\"requestType\":\"DATA\","
				+ "\"attributes\":{\"areaCode\":\"0000007525\",\"personCode\":\""+ personCode+ "\",\"visitorType\":0,\"businesserCode\":\"54\",\"parkCode\":\"0000007525\","
				+ "\"timeDesc\":{\"sd\":\""+sd+"\",\"ed\":\""+ed+"\",\"st\":\""+st+"\",\"et\":\""+et+"\",\"w\":\"1111111\"}},"
				+ "\"dataItems\":[{\"attributes\":{\"visitorName\":\""+visitorName+"\",\"visitorTel\":\""+visitorTel+"\"}}]}";
		// 生成MD5签名
		String sn = "";
		String res = "";
		try {
			MessageDigest md5Tool = MessageDigest.getInstance("MD5");
			byte[] md5Data = md5Tool.digest((p + signKey).getBytes("UTF-8"));
			sn = HttpUtils.toHexString(md5Data);
			params.put("cid", cid);
			params.put("v", v);
			params.put("p",  p);
			params.put("sn", sn);
			params.put("tn", token);
			res = HttpUtils.httpPost(url, params);
			//map=HttpUtils.jsonStr2Map(res);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}
	
	//获取token
	@Override
	public String getToken() {
		String url ="http://www.jslife.com.cn/jsaims/login?cid="+cid+"&usr="+usr+"&psw="+psw;
    	Map<String,String> map = null;
    	String token = "";
    	try {
    		String result = HttpUtils.httpGet(url, "UTF-8");
			map=HttpUtils.jsonStr2Map(result);
			token=map.get("token");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return token;
	}

	@Override
	public List<ProjectDoor> selectAllByCombox(String pid) {
		Map<String,Object> params=new HashMap<String,Object>();
		if(pid!=null&&!"".equals(pid)){
			params.put("pid", pid);
		}
		return projectDoorMapper.selectAll(params);
	}
	
}
